package com.jk.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jk.dto.TemPosition;
import com.jk.pojo.ResponseResult;
import com.jk.service.TemPositionServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author yzf
 * @version 1.0
 * @ClassName ExcelController
 * @program: office
 * @description: TODO
 * @date 2025/2/25 20:02
 */
@Api
@Slf4j
@RestController
@RequestMapping("excel")
public class ExcelController {
    @Autowired
    private TemPositionServiceFeign temPositionServiceFeign;

    @ApiOperation(value = "信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody TemPosition temPosition, HttpServletResponse response){
        List<TemPosition> JobOneList = temPositionServiceFeign.findTemPosition(temPosition);
        //.xlsx格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setContentType("application/octet-stream; charset=UTF-8");


        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //设置sheet
            ExportParams params = new ExportParams();

            params.setSheetName("职位信息表");
            //设置标题
            params.setTitle("职位信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, TemPosition.class, JobOneList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

       @ApiOperation(value = "学生信息导入", httpMethod = "POST", notes = "信息导入")
       @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(2);
            List<TemPosition> list = ExcelImportUtil.importExcel(file.getInputStream(), TemPosition.class, params);
            list.forEach(temPosition -> {

                temPositionServiceFeign.addTem(temPosition);
            });
            return new ResponseResult(200, "导入信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入信息失败！");
        }
    }


}
